package com.xy.core.utils;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by 汤伟杰 on 2017/5/8.
 */
public class CommonHttp {
    public static Map<String, Object> send(String uri, String method, Map<String,String> params) throws Exception {
        if(params==null){
            params = new HashMap<String, String>();
        }
        String info = null;
        HttpClient httpclient = new HttpClient();//方法调用
       if("get".equals(method.trim().toLowerCase())){
           int i=0;
           for (Map.Entry<String, String> entry : params.entrySet()) {
               if(i==0){
                   uri+="?"+entry.getKey()+"="+entry.getValue()+"&";
               }else{
                   uri+=entry.getKey()+"="+entry.getValue()+"&";
               }
               if(i==params.size()){
                   uri = uri.substring(0, uri.length() - 1);
               }
               i++;
           }
           System.out.println(uri);
               GetMethod get = new GetMethod(uri);
           httpclient.executeMethod(get);
           info = new String(get.getResponseBody(),"UTF-8");
           Map<String,Object> map = JSONObject.parseObject(info, Map.class);
           return map;
       }else if("post".equals(method.trim().toLowerCase())){
           PostMethod post = new PostMethod(uri);//接口地址
           post.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,"UTF-8");//格式转换
           for (Map.Entry<String, String> entry : params.entrySet()) {
               post.addParameter(entry.getKey(), entry.getValue());//接口用户名
           }
           httpclient.executeMethod(post);
           info = new String(post.getResponseBody(),"UTF-8");
           Map<String,Object> map = JSONObject.parseObject(info, Map.class);
           return map;
       }else{
           throw new Exception("请求的method只能是get或者post！");
       }

    }
    public static String sendToJson(String uri, String method, Map<String,String> params) throws Exception {
        Map<String,Object> map = send(uri, method, params);
        return JSONObject.toJSONString(map);
    }

   public static void main(String[] args) throws Exception {
      /*   for (int i = 0; i < 5; i++) {
            new Thread(new Runnable() {
                String url = "http://www.51d1d.com/chezhu/weixin/updateRedpack.jn?redpackId=oiLtyZNVLU9i8AJr0N8&openId=oRYMns_SBJTs5-FQREG3aACMSE-Q";

                public void run() {
                    try {
                        System.out.println( CommonHttp.sendToJson(url,"get",null));
                    } catch (Exception e) {
                        e.printStackTrace();
                        //{"result":"失败","status":0,"code":"FATAL_ERROR","msg":"更换了红包金额或者红包个数，但商户单号未更新"}
//{"result":"失败","status":0,"code":"NO_AUTH","msg":"发放失败，此请求可能存在风险，已被微信拦截"}
                    }
                }
            }).start();
        }*/

       for (int i = 0; i < 50; i++) {
           new Thread(new Runnable() {
               String url = "http://localhost:8080/morefun/api/checkVcode.do?ck=Yz2clFpVyhj7E7MuoMBPrg==&tk=1234&vcode=2634";

               public void run() {
                   try {
                       System.out.println( CommonHttp.sendToJson(url,"get",null));
                   } catch (Exception e) {
                       e.printStackTrace();
                   }
               }
           }).start();
       }
    }

}
